package echo;

import java.rmi.Naming;
import java.rmi.RMISecurityManager;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.server.UnicastRemoteObject;


 public class EchoServer extends UnicastRemoteObject implements Echo {
//public class EchoServer implements Echo {
	static final long serialVersionUID = -1L;
	int i = 0;

	public EchoServer() throws RemoteException {
		super();
	}

	public TodaySaying echo() throws RemoteException {
		TodaySaying ts = new TodaySaying();
		String message = "Hest";
		
		message += " " + ++i;
		System.err.println("Echo: " + message);
		
		ts.sayingOfToday = message;
		ts.count = i;
		
		return ts;
	}

	public static void main(String[] args) throws Exception {
		if (System.getSecurityManager() == null) {
			System.setSecurityManager(new RMISecurityManager());
		}
		String host = "localhost";
		if (args.length > 0) {
			host = args[0];
		}
		System.out.println("Starting EchoServer");
		
		
		EchoServer echo = new EchoServer();
		Naming.rebind("//" + host + "/EchoServer", echo);
		
		
//        String name = "//" + host + "/EchoServer";
//        
//        Echo echo2 = new EchoServer();
//        Echo stub =
//            (Echo) UnicastRemoteObject.exportObject(echo2, 0);
//        //Registry registry = LocateRegistry.getRegistry();
//        Naming.rebind(name, stub);
		
		
		
	}
}
